package com.liu.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.goods.dao.GoodsDao;
import com.liu.goods.entity.GoodsEntity;
import com.liu.goods.entity.GoodsPngEntity;
import com.liu.goods.entity.StockEntity;
import com.liu.goods.service.GoodsPngService;
import com.liu.goods.service.GoodsService;
import com.liu.goods.service.StockService;
import com.liu.goods.vo.GoodsAddVo;
import com.liu.shop.common.constant.RabbitConstant;
import com.liu.shop.common.utils.PageUtils;
import com.liu.shop.common.utils.Query;
import com.liu.shop.common.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service("goodsService")
@Transactional
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsPngService goodsPngService;

    @Autowired
    private StockService stockService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 添加商品，添加成功后将商品的所有信息存入到mq中，并发布一个消息，等待监听消费
     * @param goodsAddVo
     */
    @Override
    public void addGoods(GoodsAddVo goodsAddVo) {
        // 1.把vo转化为goodsEntity
        GoodsEntity goodsEntity = new GoodsEntity();
        BeanUtils.copyProperties(goodsAddVo,goodsEntity);

        // 2.将商品信息存入表中
        baseMapper.insert(goodsEntity);//存入成功主键回填

        // 3.插入商品图片
        List<GoodsPngEntity> goodsPngEntityList = goodsAddVo.getGoodsPngList();

        for (GoodsPngEntity goodsPngEntity : goodsPngEntityList) {
            goodsPngEntity.setGid(goodsEntity.getId());
        }
        goodsPngService.saveBatch(goodsPngEntityList);

        // 4.插入库存
        StockEntity stockEntity = new StockEntity();
        stockEntity.setGid(goodsEntity.getId());
        stockEntity.setWid(1);
        stockEntity.setStock(goodsAddVo.getStock());
        stockService.save(stockEntity);

        // 5.把新添加的商品发送给mq，通过mq来发送并存入到es
        goodsAddVo.setId(goodsEntity.getId());
        String goodsVoJson = JSON.toJSONString(goodsAddVo);
        log.debug("goodsVoJson:{}",goodsVoJson);
        rabbitTemplate.convertAndSend(RabbitConstant.GOODS_EXCHANGE,RabbitConstant.ROUTE,goodsVoJson);

    }

    @Override
    public GoodsVo getGoodsVoById(Integer id) {
        return baseMapper.getGoodsVoById(id);
    }

    @Override
    public List<GoodsAddVo> getGoodsVoList() {
        return baseMapper.getGoodsVoList();
    }

}